package com.dap.dubbo.rpc.config;

import com.dap.dubbo.rpc.utils.ZkManager;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ZkConfig implements ApplicationContextAware , BeanDefinitionRegistryPostProcessor {
    public static ApplicationContext applicationContext;
    private String host;
    public void setHost(String host) {
        this.host = host;
    }
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        BeanDefinition zkManager = BeanDefinitionBuilder.rootBeanDefinition(ZkManager.class)
                .addConstructorArgValue(host).getBeanDefinition();
        zkManager.setScope("singleton");       //设置scope
        registry.registerBeanDefinition("zKManager", zkManager);
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
